package eventplanner.entities;

import eventplanner.validation.URL;
import eventplanner.validation.URLValidator;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Test;
import org.junit.runner.RunWith;

import javax.inject.Inject;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import java.util.Set;

import static eventplanner.entities.EventTestUtil.createValidSimpleEvent;
import static junit.framework.Assert.assertEquals;

@RunWith(Arquillian.class)
public class EventIT {

	/**
	 * Constructs a deployment archive
	 *
	 * @return the deployment archive
	 */
	@Deployment
	public static Archive<?> createTestArchive() {
		return ShrinkWrap.create(WebArchive.class, "test.war")
				.addClasses(Doelgroep.class, Event.class, Identifiable.class, URL.class, URLValidator.class, EventTestUtil.class, EventType.class)
						// enable JPA
				.addAsResource("META-INF/test-persistence.xml", "META-INF/persistence.xml")
						// add sample data
//                .addAsResource("import.sql")
						// enable CDI
				.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
						// Deploy our test datasource
				.addAsWebInfResource("test-ds.xml", "test-ds.xml");
	}

	@Inject
	Validator validator;

	@Test
	public void testRegisterEventWithViolations() throws Exception {
		Event event = new Event();
		Set<ConstraintViolation<Event>> violations = validator.validate(event);
		assertEquals("Incorrect number of violations where found", 3, violations.size());
	}

	@Test
	public void testRegisterEventWithoutViolations() throws Exception {
		Event event = createValidSimpleEvent();
		Set<ConstraintViolation<Event>> violations = validator.validate(event);
		assertEquals("The user has violations", 0, violations.size());
	}

	@Test
	public void testValidURL() throws Exception {
		Event event = createValidSimpleEvent();
		event.setUrl("http://www.google.nl");
		Set<ConstraintViolation<Event>> violations = validator.validate(event);

		assertEquals("No violations expected", 0, violations.size());
	}

	@Test
	public void testInvalidURL() throws Exception {
		Event event = createValidSimpleEvent();
		event.setUrl("asdf");
		Set<ConstraintViolation<Event>> violations = validator.validate(event);

		assertEquals("Should have one violation; the url", 1, violations.size());
	}
}
